package com.example.blood_sugar.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.constraint.ConstraintLayout;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.blood_sugar.R;
import com.example.blood_sugar.bean.BSRecord;
import com.example.blood_sugar.interfaces.OnBPRecordClickListener;
import com.example.blood_sugar.interfaces.OnHbAlcRecordClickListener;
import com.example.blood_sugar.utils.DateUtil;
import com.example.blood_sugar.utils.DensityUtils;

/**
 * 自定义的显示一行糖化血红蛋白记录的view
 * 和BPRecordRowView差不多
 */
public class HbAlcRecordRowView extends ConstraintLayout {

    private View mView;
    private TextView mTextView0;
    private TextView mTextView1;

    private BSRecord mBSRecord;

    private OnHbAlcRecordClickListener mListener;

    public HbAlcRecordRowView(Context context) {
        super(context);
        initView(context);
    }

    public HbAlcRecordRowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HbAlcRecordRowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HbAlcRecordRowView);
        initView(context);

        setText0(typedArray.getString(R.styleable.HbAlcRecordRowView_hbalc_text0));
        setText1(typedArray.getString(R.styleable.HbAlcRecordRowView_hbalc_text1));

        // 用完记得要recycle();
        typedArray.recycle();

    }

    private void initView(Context context) {

        mView = LayoutInflater.from(context).inflate(R.layout.glycated_hemoglobin_record_item, this);
        mTextView0 = mView.findViewById(R.id.textView0);
        mTextView1 = mView.findViewById(R.id.textView1);
        mTextView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                    mListener.OnTextView1Click(mBSRecord);
            }
        });
    }

    private SpannableString formatSpannableString(String str) {
        if (TextUtils.isEmpty(str)){
            str = "01月01日";
        }
        SpannableString spannableString = new SpannableString(str);
        return spannableString;
    }

    private void formatString(TextView textView, String str, double low, double high) {
        if (str == null) {
            str = "0";
        }
        double d = Double.valueOf(str);
        if (d > high) {
            textView.setTextColor(Color.RED);
            textView.setText(str);
        } else if (d >= low && d <= high) {
            textView.setTextColor(Color.GREEN);
            textView.setText(str);
        } else if (d > 0 && d < low){
            textView.setTextColor(Color.BLUE);
            textView.setText(str);
        } else {
            SpannableString spannableString = new SpannableString(" ");
            Drawable drawable = getResources().getDrawable(R.drawable.add);
            drawable.setBounds(0, 0, DensityUtils.dp2px(getContext(), 11), DensityUtils.dp2px(getContext(), 11));
            ImageSpan imageSpan = new ImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
        }
    }

    public void setText0(String text) {
        mTextView0.setText(formatSpannableString(text));
    }

    public void setText1(String text) {
        formatString(mTextView1, text, 6.0, 7.0);
    }

    public void setBSRecord(BSRecord bsRecord) {
        this.mBSRecord = bsRecord;
        setText0(DateUtil.date2Str(mBSRecord.getDate(), "MM月dd日"));
        setText1(String.valueOf(bsRecord.getSaccharificationValue()));
        this.invalidate();
    }

    public void setOnHbAlcRecordClickListener(OnHbAlcRecordClickListener listener) {
        mListener = listener;
    }

    public BSRecord getBSRecord() {
        return mBSRecord;
    }
}
